package com.PuzGame.combinenumbers.customViews;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.PuzGame.combinenumbers.R;
import com.PuzGame.combinenumbers.RationalNumber;

public class RationalView extends LinearLayout {

	public RationalNumber rn_data;
	TextView Natural;
	TextView Numerator, Denominator;
	LinearLayout RationalNumberView;
	View SeperatorView;
	
	public RationalView(Context context, RationalNumber rn){
		super(context);
		rn_data = rn;
		
		
		this.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		SetAsNotSelected();
		this.setGravity(Gravity.CENTER);
		
		if(rn_data.getNumerator()/rn_data.getDenominator()!=0 || rn_data.getNumerator() == 0)
		{
			Natural = new TextView(context);
			Natural.setText(Long.toString(rn_data.getNumerator()/rn_data.getDenominator()));
			this.addView(Natural);
		}
		
		if(rn_data.getNumerator()%rn_data.getDenominator()!=0)
		{
			if(Natural == null && rn_data.iSNegative())
			{
				Natural = new TextView(context);
				Natural.setText("-");
				this.addView(Natural);
			}
			Numerator = new TextView(context);
			Numerator.setText(Long.toString(Math.abs(rn_data.getNumerator()%rn_data.getDenominator())));
			Numerator.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
			
			
			SeperatorView = new View(context);
			SeperatorView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,2));

			
			Denominator = new TextView(context);
			Denominator.setText(Long.toString(Math.abs(rn_data.getDenominator())));
			Denominator.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
			
			RationalNumberView = new LinearLayout(context);
			RationalNumberView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
			RationalNumberView.setOrientation(LinearLayout.VERTICAL);
			RationalNumberView.addView(Numerator);
			RationalNumberView.addView(SeperatorView);
			RationalNumberView.addView(Denominator);
			RationalNumberView.setGravity(Gravity.CENTER_HORIZONTAL);
			
			this.addView(RationalNumberView);
			
		}
		this.setTextColor(Color.WHITE);
	}

	public void SetAsNotSelected() {
		this.setBackgroundResource(R.drawable.blue_button);
	}
	public void SetAsSelected() {
		this.setBackgroundResource(R.drawable.blue_button_selected);
	}

	public void setTextColor(int color) {
		if(Natural != null)
		{
			Natural.setTextColor(color);
		}
		
		if(Numerator != null)
		{
			Numerator.setTextColor(color);
		}
		
		if(Denominator != null)
		{
			Denominator.setTextColor(color);
		}
		
		if(SeperatorView != null)
		{
			SeperatorView.setBackgroundColor(color);
		}
	}

	public void setTextSize(int size) {
		if(Natural != null)
		{
			Natural.setTextSize(size);
		}
		
		if(Numerator != null)
		{
			Numerator.setTextSize((float) (size/2));
		}
		
		if(Denominator != null)
		{
			Denominator.setTextSize((float) (size/2));
		}
		
		/*
		if(SeperatorView != null)
		{
			SeperatorView.setBackgroundColor(color);
		}
		*/
		
	}
	
}
